package com.haieros.www.purerunshowsystem.service;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;

import java.io.IOException;

/**
 * Created by Administrator on 2017/11/22 0022.
 */

public class MusicService extends Service {
    // 用于播放音乐等媒体资源
    private MediaPlayer mediaPlayer;
    // 当前播放音乐标记
    private int mPosition = -1;
    //标志判断播放歌曲是否是停止之后重新播放，还是继续播放
    private boolean isStop = true;

    @Override
    public void onCreate() {
        super.onCreate();
        if (mediaPlayer == null) {
            mediaPlayer = new MediaPlayer();
            /**
             * 播放结束监听
             */
            mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mp) {
                    //发送广播到MainActivity
                    Intent intent = new Intent();
                    intent.setAction("com.complete");
                    sendBroadcast(intent);
                }
            });
        }
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        try {
            if (mediaPlayer == null) {
                mediaPlayer = new MediaPlayer();
            }
            if (mPosition == intent.getIntExtra("position", 0)) {
                // 俩次点击同一首   暂停/播放
                if (mediaPlayer.isPlaying()) {
                    mediaPlayer.pause();
                } else {
                    mediaPlayer.start();
                }
            } else {
                mPosition = intent.getIntExtra("position", 0);
                // 不同歌曲
//                if (mediaPlayer.isPlaying()) {
                    playeStop();
                    playerStart(intent.getStringExtra("url"));
//                } else {
//                    playerStart(intent.getStringExtra("url"));
//                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return START_NOT_STICKY;
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    public void playerStart(String path) {
        if (mediaPlayer == null) {
            mediaPlayer = new MediaPlayer();
        }
        try {
            mediaPlayer.setDataSource(path);
            mediaPlayer.prepare();
            mediaPlayer.start();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void playeStop() {
        try {
            if (mediaPlayer == null) {
                mediaPlayer = new MediaPlayer();
            }
            if (mediaPlayer.isPlaying()) {
                mediaPlayer.stop();
                mediaPlayer.reset();
                mediaPlayer.release();
                mediaPlayer = new MediaPlayer();
            }else {
                mediaPlayer = new MediaPlayer();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
